JavaScript provides various data types to hold different kinds of values. These data types can be categorized into primitive and non-primitive types, each serving unique purposes in coding.
Primitive data types are the most basic data types in JavaScript. They include:
This type represents both integer and floating-point numbers. JavaScript uses a double-precision 64-bit binary format (IEEE 754) for all numeric values. This means that it can handle a range of numbers, from very large to very small, but it may lose precision when dealing with very large integers or performing arithmetic operations that exceed its precision limit.
Strings are sequences of characters used to represent text. They can be enclosed in single quotes, double quotes, or backticks (template literals). Strings are immutable, meaning once created, they cannot be changed. Instead, any operation that appears to modify a string will actually create a new string.
This type represents a logical entity and can have two values: true
and false
.
Booleans are typically used for conditional testing in control flow statements like if
statements.
A variable that has been declared but not assigned a value is of type undefined
. This
indicates the absence of a value, and it’s important to differentiate it from null
, which
represents an intentional absence.
This type represents the intentional absence of any object value. It is a primitive value that indicates
"no value" or "empty value." Developers often use null
to signify that a variable should
have no value at a certain point.
Introduced in ES6, Symbol
is a unique and immutable primitive value. It is often used as the
key of an object property to avoid name clashes, since each symbol is guaranteed to be unique, even if
they are created with the same description.
Introduced in ES2020, BigInt
allows representation of whole numbers larger than
253 - 1
, which is the largest number JavaScript can accurately represent with
the Number
type. BigInt
can be created by appending n
to the end
of an integer or using the BigInt()
function.
let num = 42; // Number
let str = "Hello, World!"; // String
let bool = true; // Boolean
let undef; // Undefined
let nul = null; // Null
let sym = Symbol("unique"); // Symbol
let bigInt = 9007199254740991n; // BigInt
Non-primitive data types are objects and can hold collections of values and more complex entities. They include:
Objects are collections of key-value pairs where keys are strings (or Symbols) and values can be of any data type. Objects can represent complex data structures, such as a user profile containing various properties like name, age, and email.
Arrays are ordered collections of values. In JavaScript, arrays can hold items of any type and are
dynamically sized. They come with a rich set of built-in methods for manipulation, such as
push
, pop
, shift
, and map
.
Functions in JavaScript are first-class objects, meaning they can be assigned to variables, passed as arguments, and returned from other functions. They encapsulate reusable blocks of code and can accept parameters and return values.
The Date
object provides methods for working with dates and times. It can be instantiated
with the current date and time or a specific date, and it includes various methods for extracting date
components like year, month, day, etc.
Maps are collections of keyed data items, similar to objects. However, they allow keys of any type (not just strings) and maintain the insertion order of keys. Maps come with methods for setting, getting, and deleting entries, making them useful for associative arrays.
Sets are collections of unique values. They can store any type of value, but duplicates are not allowed. Sets come with methods for adding, deleting, and checking for values, making them useful for maintaining collections of distinct items.
A variant of Map that allows only objects as keys and does not prevent garbage collection. When there are no other references to the key object, the entry in the WeakMap can be removed automatically, making it suitable for memory management in certain scenarios.
Similar to Set, but it only allows objects as values and does not prevent garbage collection. When an object in a WeakSet is no longer referenced elsewhere, it can be garbage collected, making it useful for certain memory management techniques.
let obj = { name: "John", age: 30 }; // Object
let arr = [1, 2, 3, 4, 5]; // Array
function greet() { console.log("Hello!"); } // Function
let date = new Date(); // Date
let map = new Map(); // Map
let set = new Set(); // Set
let weakMap = new WeakMap(); // WeakMap
let weakSet = new WeakSet(); // WeakSet